<?php

function removeExtension($filename)
{
  if ($filename == null)
  {
    return $filename;
  }
  
  $index = strripos($filename,".");
  
  if ($index === 0)
  {
    return "";
  }
  elseif ($index == null)
  {
    return $filename;
  }
  else
  {
    return substr($filename,0,$index);
  }
}

function createFlash($input,$output,$text)
{
  saveFile($input,$text);
  $answer = exec("java -jar grin.jar {$input} {$output}");
  return $answer;
}

function saveFile($filename,$text)
{
  $file = fopen($filename, 'w');
  fwrite($file, $text);
  fclose($file);
}

function nextFilename($filename)
{
  
  if ($filename <> "")
  {
    return removeExtension($filename);
  }
  
  $name = "grin/newfile";
  $random = "";
  $ext1 = ".grin";
  $ext2 = ".swf";
  
  while (file_exists($name . $random . $ext1) || file_exists($name . $random . $ext2))
  {
    $random = "" . rand(1,1000000000);
  }
  return $name . $random;
}


?>
